package com.m3958.firstgwt.client.ui;

import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.inject.Singleton;

@Singleton
public class ActiveIndicator {
	
	private final PopupPanel panel; 
	
	private Timer timeoutTimer = null;
	
	private final Label label;
	
	public ActiveIndicator() {
		panel = new PopupPanel(false);
		label = new Label();
		panel.setWidget(label);
		panel.setStyleName("indicator-panel");
		panel.setPopupPosition((Window.getClientWidth() - panel.getOffsetWidth())/2, 0);
	}
	
	public void show(){
		label.setText("活动中...");
		panel.show();
	}
	
	public void show(String s){
		label.setText(s + "...");
		panel.show();
	}
	
	public void show(String s,int seconds){
		label.setText(s + "...");
		panel.show();
		cancelTimer();
		timeoutTimer = new Timer() {
			public void run() {
				hide();
				timeoutTimer = null;
			}
		};
		timeoutTimer.schedule(seconds * 1000);
	}

	  private void cancelTimer() {
		  if (timeoutTimer != null) {
			  timeoutTimer.cancel();
			  timeoutTimer = null;
		  }
	  }
	
	public void hide(){
		panel.hide();
	}
}
